package com.base.recycleView.adapter;

/**
 * Created by gml on 17/5/25.
 */

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.base.recycleView.activity.BindingViewHolder;
import com.modules.bean.User;
import com.view.multiple.ItemTyp1;

import java.util.ArrayList;
import java.util.List;

public class MultipleAdapter<T> extends RecyclerView.Adapter<BindingViewHolder> {

    protected ArrayList<T> mEmployeeList;


    protected final LayoutInflater mLayoutInflater;


    private static final int ITEM_VIEW_TYPE_ON = 1;
    private static final int ITEM_VIEW_TYPE_OFF = 2;


    private BaseReceycleAdapter.OnItemClickListener mListener;


    //
    private MultipleItemManager mItemManager;

    public MultipleAdapter(Context context) {

        mEmployeeList = new ArrayList<>();
        mLayoutInflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mItemManager = new MultipleItemManager();
        mItemManager.addItem(new ItemTyp1());//增加默认类型
    }


    @Override
    public BindingViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        ViewDataBinding binding = DataBindingUtil.inflate(mLayoutInflater,
                mItemManager.getItemViewId(viewType), parent, false);

        return new BindingViewHolder(binding);
    }


    @Override
    public void onBindViewHolder(BindingViewHolder holder, int position) {
        final T employee = mEmployeeList.get(position);
        holder.getBinding().setVariable(com.gml.gao.BR.item, employee);
        holder.getBinding().executePendingBindings();
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onEmployeeClick(employee);
                }
            }
        });
    }


    @Override
    public int getItemViewType(int position) {
        int  tempTyp =mItemManager.getItemViewType((User) mEmployeeList.get(position));
        if(tempTyp==0){//默认为一种类型
            tempTyp= mItemManager.DEFALTE_TYEP;
        }
        return tempTyp;
    }

    @Override
    public int getItemCount() {
        return mEmployeeList.size();
    }


    public void add(T employee) {
        mEmployeeList.add(employee);
        notifyDataSetChanged();
    }

    public void remove(T employee) {
        mEmployeeList.remove(employee);
        notifyDataSetChanged();
    }


    public void addAll(ArrayList<T> employees) {
        mEmployeeList.addAll(employees);
        notifyDataSetChanged();

    }


}
